/*
 * @Description: string
 * @Author: klisit
 * @Date: 2021-01-10 17:09:58
 * @LastEditTime: 2021-04-26 11:34:48
 * @LastEditors: klisit
 */
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { exec } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './filters/http-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  app.disable('x-powered-by'); //屏蔽后端技术

  app.enableCors(); //开启跨域

  app.useGlobalFilters(new HttpExceptionFilter()); //异常过滤器

  app.setGlobalPrefix('api'); //公用后缀

  /**
   * 文档生成* * * * * * * * * * * * * * * * * * * * * * * *
   */
  if (process.env.DOCUMENT_STATUS == '1') {
    const options = new DocumentBuilder()
      .setTitle('api document')
      .setDescription('is back api doc')
      .setVersion('0.0.1')
      .addBearerAuth()
      .build();
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('api-doc', app, document);
  }

  /* * * * * * * * * * * * * * * * * * * * * * * * * * * */

  await app.listen(process.env.DEV_PROT, () => {
    if (process.env.NODE_ENV == 'production') {
      /**
       * VERSION * * * * * * * * * * * * * * * * * * * * * * * *
       */
      const appConfigPath = join(__dirname, '../package.json');
      const prettierPath = join(__dirname, '../node_modules/.bin/prettier');
      const appString = readFileSync(appConfigPath).toString();
      const appConfig = JSON.parse(appString);
      appConfig.version = process.env.VERSION;
      const appBuffer = Buffer.from(JSON.stringify(appConfig), 'utf8');
      writeFileSync(appConfigPath, appBuffer);

      exec(`${prettierPath} --write ${appConfigPath}`, (error, stdout, stderr) => {
        if (error) {
          console.log('\x1B[41;30m FAIL \x1B[0;31m 格化错误 \x1B[0m %s', stderr);
          console.log(error);
        } else {
          console.log('\x1B[42;30m DONE \x1B[0;32m format %s \x1B[0m', stdout);
        }
      });
      /* * * * * * * * * * * * * * * * * * * * * * * * */
    }

    console.log(
      '\x1B[0;31m      ___            ___            ___      __________      ___      __________\n     /  /|          /  /|          /  /|    /  _______/|    /  /|    /___   ___/|\n    /  / /         /  / /         /  / /   /  /______ |/   /  / /    |__/  /|__|/\n   /  /_/____     /  / /         /  / /   /______   /|    /  / /       /  / /\n  /     ____/|   /  / /         /  / /    |_____/  / /   /  / /       /  / /\n /  /\\  \\___|/  /  /_/____     /  / /   _______/  / /   /  / /       /  / /\n/__/ |\\__\\     /_________/|   /__/ /   /_________/ /   /__/ /       /__/ /\n|__|/ \\|__|    |_________|/   |__|/    |_________|/    |__|/        |__|/   \x1B[0;30m version: 0.0.0\n \x1B[0m',
    );
    if (process.env.DOCUMENT_STATUS == '1') {
      console.log('\x1B[42;30m DONE \x1B[0;36m http://localhost:%s/api-doc \x1B[0m', process.env.DEV_PROT);
    }
  });
}
bootstrap();
